package cn.com.klun.tjy.common.listener;

import cn.com.klun.tjy.common.cache.GlobalApplicationCache;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.io.*;

public class PropertiesConfigListener implements ServletContextListener {
    private Logger logger = LoggerFactory.getLogger("PropertiesConfigListener");
    private String propertiesConfigLocation;

    public PropertiesConfigListener() {
    }

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        try {
            this.propertiesConfigLocation = sce.getServletContext().getInitParameter("propertiesConfigLocation").trim();
            this.logger.info("监听器加载配置文件开始，文件名称如下：{}", this.propertiesConfigLocation);
            if (StringUtils.isNotEmpty(this.propertiesConfigLocation)) {
                String[] paths = this.propertiesConfigLocation.split(",");
                InputStream is = null;
                BufferedReader br = null;
                String lineStr = null;
                //int index = false;
                String[] var9 = paths;
                int var10 = paths.length;

                for(int var11 = 0; var11 < var10; ++var11) {
                    String path = var9[var11];
                    is = this.getClass().getResourceAsStream(path);
                    br = new BufferedReader(new InputStreamReader(is));

                    while((lineStr = br.readLine()) != null) {
                        if (!lineStr.startsWith("#")) {
                            int index = lineStr.indexOf("=");
                            if (index > 0) {
                                String key = lineStr.substring(0, index).trim();
                                String value = lineStr.substring(index + 1, lineStr.length()).trim();
                                this.logger.debug("##########key=" + key + ",value=" + value);
                                GlobalApplicationCache.getInstance().put(key, value);
                            }
                        }
                    }
                }
            }
        } catch (FileNotFoundException var13) {
            this.logger.error("监听器加载配置文件，文件示找到异常", var13);
        } catch (IOException var14) {
            this.logger.error("监听器加载配置文件，读取文件异常", var14);
        }

    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
    }
}